package org.testing.dbunit.extension.connection;

import java.util.LinkedList;

public class Exceptions {
    public static RuntimeException cause(Exception exception) {
        Throwable root = getRootCause(exception);
        if (root instanceof RuntimeException) {
    	return (RuntimeException) root;
        }
        return new RuntimeException(root);
    }

    private static Throwable getRootCause(Throwable cause) {
        LinkedList<Throwable> linked = new LinkedList<Throwable>();
        linked.add(cause);
        while (cause.getCause() != null) {
    	linked.add(cause.getCause());
    	cause = cause.getCause();
        }
        return linked.getLast();
    }
}